package com.zhai.domain.strategy.service.rule.chain.factory;

import com.zhai.domain.strategy.model.entity.StrategyEntity;
import com.zhai.domain.strategy.repository.IStrategyRepository;
import com.zhai.domain.strategy.service.rule.chain.ILogicChain;
import lombok.*;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description: 默认责任链工厂
 * @ClassName: DefaultChainFactory
 * @Author: zhaiyongxin
 * @Date: 2025/1/9 15:10
 * @Version: 1.0
 */
@Service
public class DefaultChainFactory {

    private final Map<String, ILogicChain> logicChainGroup;
    private final IStrategyRepository repository;

    public DefaultChainFactory(Map<String, ILogicChain> logicChainGroup, IStrategyRepository repository) {
        this.logicChainGroup = logicChainGroup;
        this.repository = repository;
    }

    public ILogicChain openLogicChain(Long strategyId) {
        StrategyEntity strategy = repository.queryStrategyEntityByStrategyId(strategyId);
        String[] ruledModels = strategy.ruleModels();
        if (ruledModels == null || ruledModels.length == 0) {
            return logicChainGroup.get("default");
        }
        ILogicChain logicChain = logicChainGroup.get(ruledModels[0]);
        ILogicChain current = logicChain;
        for (int i = 1; i < ruledModels.length; i++) {
            ILogicChain next = logicChainGroup.get(ruledModels[i]);
            current = current.appendNext(next);
        }
        current.appendNext(logicChainGroup.get("default"));
        return logicChain;
    }

    @AllArgsConstructor
    @Data
    @Builder
    public static class StrategyAwardVO {
        /** 抽奖奖品ID - 内部流转使用 */
        private Integer awardId;
        /**  */
        private String logicModel;
        private String awardRuleValue;
    }
    @Getter
    @AllArgsConstructor
    public enum LogicModel {
        RULE_DEFAULT("rule_default", "默认抽奖"),
        RULE_BLACKLIST("rule_blacklist", "黑名单抽奖"),
        RULE_WEIGHT("rule_weight", "权重规则"),
        ;
        private final String code;
        private final String info;
    }
}
